<template>
  <div class="news-detail-page">
    <div v-if="loading" class="loading-container">
      <a-spin size="large" />
    </div>
    
    <template v-else-if="news">
      <div class="news-header">
        <h1 class="news-title">{{ news.title }}</h1>
        <div class="news-meta">
          <span class="meta-item"><user-outlined /> {{ news.author }}</span>
          <span class="meta-item"><calendar-outlined /> {{ formatDate(news.publishTime) }}</span>
          <span class="meta-item"><eye-outlined /> {{ news.viewCount }} 次浏览</span>
        </div>
      </div>
      
      <div class="news-content">
        <!-- 新闻图片 -->
        <div v-if="news.coverImage" class="news-image">
          <img :src="`/api/files/${news.coverImage}`" :alt="news.title" />
        </div>
        
        <!-- 新闻正文 -->
        <div class="news-body" v-html="news.content"></div>
        
        <!-- 视频内容 -->
        <div v-if="news.videoUrl" class="news-video">
          <video controls>
            <source :src="news.videoUrl" type="video/mp4">
            您的浏览器不支持视频播放
          </video>
        </div>
      </div>
      
      <div class="news-footer">
        <div class="news-tags" v-if="news.tags && news.tags.length">
          <span class="tag-label">标签：</span>
          <a-tag v-for="tag in news.tags" :key="tag" color="blue">{{ tag }}</a-tag>
        </div>
        
        <div class="news-actions">
          <a-button type="primary" @click="goBack">返回列表</a-button>
        </div>
      </div>
    </template>
    
    <a-result v-else status="404" title="未找到新闻" sub-title="您访问的新闻不存在或已被删除">
      <template #extra>
        <a-button type="primary" @click="goBack">返回列表</a-button>
      </template>
    </a-result>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, CalendarOutlined, EyeOutlined } from '@ant-design/icons-vue'
import dayjs from 'dayjs'
import request from '@/utils/request' // 导入配置好的 axios 实例

const props = defineProps({
  id: {
    type: [String, Number],
    required: true
  }
})

const route = useRoute()
const router = useRouter()

// 状态变量
const loading = ref(true)
const news = ref(null)

// 格式化日期
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm')
}

// 获取新闻详情
const fetchNewsDetail = async () => {
  loading.value = true
  try {
    // 实际项目中应该调用API
    const response = await request.get(`/news/frontend/detail/${props.id}`)
    news.value = response.data
    
    // 模拟数据
    await new Promise(resolve => setTimeout(resolve, 800))
    
    // 根据ID生成不同的模拟数据
    const categoryId = response.data.categoryId
    let category = '学校要闻'
    
    if (categoryId ==2 ) {
      category = '学术动态'
    } else if (categoryId ==3) {
      category = '综合新闻'
    } else if (categoryId ==4) {
      category = '通知公告'
    }
    news.value.category = category
    
  } catch (error) {
    message.error('获取新闻详情失败，请稍后再试')
    console.error('获取新闻详情失败:', error)
    news.value = null
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchNewsDetail()
})
</script>

<style scoped>
.news-detail-page {
  padding-bottom: 30px;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 400px;
}

.news-header {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 25px;
  margin-bottom: 20px;
}

.news-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  line-height: 1.4;
}

.news-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  color: #666;
  font-size: 14px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.news-content {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 25px;
  margin-bottom: 20px;
}

.news-image {
  margin-bottom: 20px;
  text-align: center;
}

.news-image img {
  max-width: 100%;
  border-radius: 4px;
}

.news-body {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
}

.news-body :deep(h3) {
  font-size: 20px;
  font-weight: bold;
  margin: 25px 0 15px;
  color: #003366;
}

.news-body :deep(p) {
  margin-bottom: 15px;
}

.news-video {
  margin-top: 20px;
}

.news-video video {
  width: 100%;
  max-width: 800px;
  display: block;
  margin: 0 auto;
  border-radius: 4px;
}

.news-footer {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.news-tags {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
}

.tag-label {
  color: #666;
  margin-right: 5px;
}

.news-actions {
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .news-title {
    font-size: 22px;
  }
  
  .news-meta {
    gap: 15px;
    font-size: 13px;
  }
  
  .news-body {
    font-size: 15px;
  }
}
</style>